using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Atechnology.glassCAD.Shapes.CutViewer
{
    public class CoordinateConverter
    {
        public static float ModelToScreen(float modelSize, RectangleF modelBoundingBox, int screenWidth, int screenHeight)
        {
            float result;
            double transformCoef = 0;

            if ((double)screenWidth / (double)screenHeight >= modelBoundingBox.Width / modelBoundingBox.Height)
                transformCoef = 0.8 * (double)screenHeight / (double)modelBoundingBox.Height;
            else
                transformCoef = 0.8 * (double)screenWidth / (double)modelBoundingBox.Width;

            result = (float)(modelSize * transformCoef);
            return result;
        }

        public static Point ModelToScreen(PointF modelPoint, RectangleF modelBoundingBox, int screenWidth, int screenHeight)
        {
            Point result = new Point();
            double transformCoef = 0;

            if ((double)screenWidth / (double)screenHeight >= modelBoundingBox.Width / modelBoundingBox.Height)
                transformCoef = 0.8 * (double)screenHeight / (double)modelBoundingBox.Height;
            else
                transformCoef = 0.8 * (double)screenWidth / (double)modelBoundingBox.Width;

            float modelOffsetX = (float)(modelBoundingBox.Width / 2 * transformCoef);
            float modelOffsetY = (float)(modelBoundingBox.Height / 2 * transformCoef);

            result.X = (int)(modelPoint.X * transformCoef + screenWidth / 2 - modelOffsetX);
            result.Y = (int)(modelPoint.Y * transformCoef);
            result.Y = screenHeight - result.Y;
            result.Y -= (int)(screenHeight / 2 - modelOffsetY);
            return result;
        }

        //public static PointF ScreenToModel(float screenSize, RectangleF modelBoundingBox, int screenWidth, int screenHeight)
        //{
        //    float result;
        //    double transformCoef = 0;

        //    if ((double)screenWidth / (double)screenHeight >= modelBoundingBox.Width / modelBoundingBox.Height)
        //        transformCoef = 0.8 * (double)screenHeight / (double)modelBoundingBox.Height;
        //    else
        //        transformCoef = 0.8 * (double)screenWidth / (double)modelBoundingBox.Width;

        //    result = (float)(modelSize / transformCoef);
        //    return result;
        //}
    }
}
